package com.lzc.service.impl;

import com.lzc.mapper.FriendMapper;
import com.lzc.mapper.MessageMapper;
import com.lzc.pojo.entity.Friend;
import com.lzc.pojo.entity.Message;
import com.lzc.pojo.vo.Result;
import com.lzc.service.MessageService;
import com.lzc.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private FriendMapper friendMapper;

    /**
     * 找到历史消息记录
     * @param rid 聊天的用户id
     * @return
     */
    @Override
    public Result findHistoricalMessages(Long rid) {
        Long sid = ThreadLocalUtil.getUid();
        // 判断是否为好友
        Friend friend = friendMapper.isFriend(sid, rid);
        if (friend == null){
            return Result.FILE("不是好友! 无法进行聊天");
        }
        // 获取历史聊天消息
        List<Message> historicalChat = messageMapper.findHistoricalChat(sid, rid);
        return Result.OK(historicalChat);
    }
}
